package com.techblor.wechat.library.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密工具类
 */
public class EncryptUtil {

    /**
     * sha1
     * @param value
     * @return
     */
    public static String sha1(String value) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(value.getBytes());
            byte[] messageDigest = digest.digest();

            // 创建十六进制字符串
            StringBuilder builder = new StringBuilder();

            // 字节数组转换为十六进制数
            for (byte b : messageDigest) {
                String shaHex = Integer.toHexString(b & 0xFF);
                if (shaHex.length() < 2) {
                    builder.append(0);
                }

                builder.append(shaHex);
            }

            return builder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return "";
    }
}
